package xm.chengdu.com.beijing_211;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import adapter.ErrAdapter;
import base.Z_Login_UserInfoDao;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import javabean.Life_Login_UserInfo;
import com.sdwfqin.cbt.Content;

/**
 * 错误
 */
public class ListerrActivity extends AppCompatActivity {
    Z_Login_UserInfoDao z_login_userInfoDao;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.lv)
    ListView lv;
    ErrAdapter errAdapter;
    boolean gx = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listerr);
        ButterKnife.bind(this);
        z_login_userInfoDao = new Z_Login_UserInfoDao(ListerrActivity.this);

        errAdapter = new ErrAdapter();
        lv.setAdapter(errAdapter);
//        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Life_Login_UserInfo item = (Life_Login_UserInfo) errAdapter.getItem(i);
//                gx = false;
//                show(i, item);
//            }
//        });
        gx = true;
        mHandler.postDelayed(r, 100);//延时100毫秒
    }


//    public void show(final int is, final Life_Login_UserInfo life_login_userInfo) {
//        new AlertDialog.Builder(this)
//
//                .setTitle("reminding")
//
//                .setMessage("Whether to Delete Data？")
//
//                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        errAdapter.removeItem(is);
//                        errAdapter.notifyDataSetChanged();
//                        z_login_userInfoDao.delete(life_login_userInfo);
//                        gx = true;
//                    }
//                })
//                .setNegativeButton("NO", null)
//
//                .show();
//    }

    Handler mHandler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            //do something
            List<Life_Login_UserInfo> life_login_userInfos = z_login_userInfoDao.queryForAll();
            Collections.reverse(life_login_userInfos);
            if (Content.zhengchang) {
                errAdapter.clear();
                errAdapter.notifyDataSetChanged();
            } else {
                if (gx) {
                    errAdapter.clear();
                    errAdapter.addItem(life_login_userInfos);
                }

            }

            //每隔1s循环执行run方法
            mHandler.postDelayed(this, 5000);
        }
    };

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
